home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Scope / Scope Disk #142 (199x)(Scope PD)(US)[WB].zip / Scope Disk #142 (199x)(Scope PD)(US)[WB].adf / LoadImage / LoadImage.c < prev    next >
C/C++ Source or Header  |  1990-07-30  |  49KB  |  2,308 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by MXM
  4.  *
  5.  *    Name .....: LoadImage.c
  6.  *    Created ..: Saturday 23-Sep-90 12:30
  7.  *    Revision .: 11
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    11-May-90       Olsen           New routines
  12.  *    01-May-90       Olsen           Rework for Aztec 5.0 release
  13.  *    23-Sep-89       Olsen           Created this file!
  14.  *
  15.  * $Revision Header ********************************************************/
  16.  
  17.     /* Arp user interface stuff. */
  18.  
  19. char *CLI_Template    = "INFO/S,NOMODE/S,LACE/S,CYCLE/S,Files,,,,,,";
  20. char *CLI_Help        = "\nUsage: LoadImage [INFO] [NOMODE] [LACE] [CYCLE] Files1 ... FileN\n\n       NOMODE and LACE are mutually exclusive.\n";
  21.  
  22.     /* Argument vector offsets. */
  23.  
  24. #define ARG_INFO    1
  25. #define ARG_NOMODE    2
  26. #define ARG_LACE    3
  27. #define ARG_CYCLE    4
  28. #define ARG_FILES    5
  29.  
  30.     /* The cycling code needs this to be defined externally. */
  31.  
  32. CRange CycleRange[6];
  33.  
  34.     /* Some more fontdata for the Requester soon to come. */
  35.  
  36. struct TextAttr StandardFont[3] =
  37. {
  38.     {(UBYTE *)"topaz.font",8,FS_NORMAL ,FPF_ROMFONT},
  39.     {(UBYTE *)"topaz.font",8,FSF_BOLD  ,FPF_ROMFONT},
  40.     {(UBYTE *)"topaz.font",8,FSF_ITALIC,FPF_ROMFONT}
  41. };
  42.  
  43.     /* Menu text definitions. */
  44.  
  45. struct IntuiText MenuIntTxt[10] =
  46. {
  47.     {0,0,0,2,1,&StandardFont[0],(UBYTE *)"About LoadImage...",    NULL},
  48.     {0,0,0,0,0,&StandardFont[1],(UBYTE *)"________________________",NULL},
  49.     {0,0,0,2,1,&StandardFont[0],(UBYTE *)"Title bar",        NULL},
  50.     {0,0,0,2,1,&StandardFont[0],(UBYTE *)"Cycling",            NULL},
  51.     {0,0,0,0,0,&StandardFont[1],(UBYTE *)"________________________",NULL},
  52.     {0,0,0,2,1,&StandardFont[0],(UBYTE *)"Print Standard",        NULL},
  53.     {0,0,0,2,1,&StandardFont[0],(UBYTE *)"      Enlarged",        NULL},
  54.     {0,0,0,0,0,&StandardFont[1],(UBYTE *)"________________________",NULL},
  55.     {0,0,0,2,1,&StandardFont[0],(UBYTE *)"Next Image",        NULL},
  56.     {0,0,0,2,1,&StandardFont[0],(UBYTE *)"Quit LoadImage",        NULL}
  57. };
  58.  
  59.     /* A chain of menu items. */
  60.  
  61. struct MenuItem MenuItem[10] =
  62. {
  63.     {&MenuItem[1],    1, 0,196,10, 86,0,(APTR)&MenuIntTxt[0],NULL,'?',NULL,NULL},
  64.     {&MenuItem[2],    3, 4,192, 8,210,0,(APTR)&MenuIntTxt[1],NULL,  0,NULL,NULL},
  65.     {&MenuItem[3],    1,13,196,10, 86,0,(APTR)&MenuIntTxt[2],NULL,'T',NULL,NULL},
  66.     {&MenuItem[4],    1,23,196,10, 86,0,(APTR)&MenuIntTxt[3],NULL,'C',NULL,NULL},
  67.     {&MenuItem[5],    3,27,192, 8,210,0,(APTR)&MenuIntTxt[4],NULL,  0,NULL,NULL},
  68.     {&MenuItem[6],    1,36,196,10, 86,0,(APTR)&MenuIntTxt[5],NULL,'S',NULL,NULL},
  69.     {&MenuItem[7],    1,46,196,10, 86,0,(APTR)&MenuIntTxt[6],NULL,'E',NULL,NULL},
  70.     {&MenuItem[8],    3,50,192, 8,210,0,(APTR)&MenuIntTxt[7],NULL,  0,NULL,NULL},
  71.     {&MenuItem[9],    1,59,196,10, 86,0,(APTR)&MenuIntTxt[8],NULL,'N',NULL,NULL},
  72.     {NULL,        1,69,196,10, 86,0,(APTR)&MenuIntTxt[9],NULL,'Q',NULL,NULL}
  73. };
  74.  
  75.     /* Our only menu. */
  76.  
  77. struct Menu Menu = { NULL,1,0,120,0,257,"LoadImage 1.11",&MenuItem[0] };
  78.  
  79.     /* Requester text definitions. */
  80.  
  81. struct IntuiText ReqIntTxt[] =
  82. {
  83.     {0,0,0, 76,  4,&StandardFont[0],(UBYTE *)"Understood",                NULL},
  84.  
  85.     {0,0,0, 94,  6,&StandardFont[1],(UBYTE *)"LoadImage 1.11",            &ReqIntTxt[2]},
  86.  
  87.     {0,0,0, 34, 18,&StandardFont[0],(UBYTE *)"Was  written  by Olaf 'Olsen'",    &ReqIntTxt[3]},
  88.     {0,0,0, 34, 26,&StandardFont[0],(UBYTE *)"Barthel of MXM.  This version",    &ReqIntTxt[4]},
  89.     {0,0,0, 34, 34,&StandardFont[0],(UBYTE *)"supports  EHB & HAM pictures,",    &ReqIntTxt[5]},
  90.     {0,0,0, 34, 42,&StandardFont[0],(UBYTE *)"will   also   take   care  of",    &ReqIntTxt[6]},
  91.     {0,0,0, 34, 50,&StandardFont[0],(UBYTE *)"oversized    pictures   which",    &ReqIntTxt[7]},
  92.     {0,0,0, 34, 58,&StandardFont[0],(UBYTE *)"require  1M  of  chip ram and",    &ReqIntTxt[8]},
  93.     {0,0,0, 46, 66,&StandardFont[0],(UBYTE *)"features an Arp interface.",        &ReqIntTxt[9]},
  94.     {0,0,0, 34, 82,&StandardFont[0],(UBYTE *)"Press  the  left mouse button",    &ReqIntTxt[10]},
  95.     {0,0,0, 34, 90,&StandardFont[0],(UBYTE *)"and move the pointer  towards",    &ReqIntTxt[11]},
  96.     {0,0,0, 34, 98,&StandardFont[0],(UBYTE *)"the borders of the  screen to",    &ReqIntTxt[12]},
  97.     {0,0,0, 34,106,&StandardFont[0],(UBYTE *)"scroll around in  overscanned",    &ReqIntTxt[13]},
  98.     {0,0,0,122,114,&StandardFont[0],(UBYTE *)"images.",                &ReqIntTxt[14]},
  99.  
  100.     {0,0,0, 58,130,&StandardFont[2],(UBYTE *)"© Copyright 1990 by MXM",        NULL}
  101. };
  102.  
  103.     /* Requester border coordinates. */
  104.  
  105. SHORT ReqBrdDat[] =
  106. {
  107.     -3,-2,234,-2,234, 17,-3, 17,-3,-2,
  108.     -6,-4,237,-4,237, 19,-6, 19,-6,-4,
  109.      0, 0,293, 0,293,172, 0,172, 0, 0,
  110.      0, 0,295, 0,295,172, 0,172, 0, 0
  111. };
  112.  
  113.     /* Requester border definitions. */
  114.  
  115. struct Border ReqBrd[] =
  116. {
  117.     {0,0,0,0,0,5,&ReqBrdDat[ 0],&ReqBrd[1]},
  118.     {0,0,0,0,0,5,&ReqBrdDat[10],NULL},
  119.     {3,1,0,0,0,5,&ReqBrdDat[20],&ReqBrd[3]},
  120.     {2,1,0,0,0,5,&ReqBrdDat[30],NULL}
  121. };
  122.  
  123.     /* The only gadget employed by the Requester. */
  124.  
  125. struct Gadget ReqGad = {NULL,34,149,232,16,0,3,4097,(APTR)&ReqBrd[0],NULL,&ReqIntTxt[0],NULL,NULL,0,NULL};
  126.  
  127.     /* Our only Requester. */
  128.  
  129. struct Requester Req;
  130.  
  131.     /* Yes... we will open a CUSTOMBITMAP screen. */
  132.  
  133. struct BitMap ScreenMap;
  134.  
  135.     /* If the picture is smaller than the screen. */
  136.  
  137. struct BitMap TinyBitMap;
  138.  
  139.     /* Must be defined externally since three routines need it.
  140.      * There could have been an easier way to do this, but it was
  141.      * me who programmed it and not a "reentrant-code-fanatic".
  142.      */
  143.  
  144. BitMapHeader InfoHeader;
  145.  
  146.     /* Empty colour information. */
  147.  
  148. UWORD BlackIsBlack[32] =
  149. {
  150.     0,0,0,0,0,0,0,0,
  151.     0,0,0,0,0,0,0,0,
  152.     0,0,0,0,0,0,0,0,
  153.     0,0,0,0,0,0,0,0
  154. };
  155.  
  156.     /* Wait and see what this will be! */
  157.  
  158. struct NewScreen NewScreen =
  159. {
  160.     0,0,0,0,0,                /* Gets filled in later. */
  161.     0,1,                    /* Pens. */
  162.     NULL,                    /* ViewModes, get filled in later. */
  163.     CUSTOMSCREEN | SCREENBEHIND | CUSTOMBITMAP,
  164.     &StandardFont[0],            /* Just insure that the menu looks cute. */
  165.     (STRPTR)"LoadImage 1.11",        /* Some title text. */
  166.     NULL,                    /* No gagdets. */
  167.     &ScreenMap                /* Custom bitmap! */
  168. };
  169.  
  170.     /* What do we need this window for? We've got a menu and
  171.      * screens don't like menus very much...
  172.      */
  173.  
  174. struct NewWindow NewWindow =
  175. {
  176.     0,0,                    /* These get filled in later. */
  177.     0,0,
  178.     0,1,                    /* Pens. */
  179.     GADGETUP | MOUSEBUTTONS | MENUPICK | MENUVERIFY | MOUSEMOVE | RAWKEY,    /* We want to know about this. */
  180.     BACKDROP | BORDERLESS | RMBTRAP,    /* This is how it should look like. */
  181.     (struct Gadget *)NULL,            /* Don't need a gadget. */
  182.     (struct Image *)NULL,            /* Don't need a checkmark either. */
  183.     (STRPTR)NULL,                /* Don't need a title. */
  184.     (struct Screen *)NULL,            /* This gets filled in later. */
  185.     (struct BitMap *)NULL,            /* Don't need it, really. */
  186.     0,0,                    /* Minimum dimensions. */
  187.     0,0,                    /* Maximum dimensions. */
  188.     CUSTOMSCREEN                /* No Workbench, this is MY window. */
  189. };
  190.  
  191.     /* External symbols the linker wants. */
  192.  
  193. extern struct IntuitionBase    *IntuitionBase;
  194. extern struct GfxBase        *GfxBase;
  195. extern struct ExecBase        *SysBase;
  196.  
  197. struct Screen            *Screen;
  198. struct Window            *Window;
  199.  
  200.     /* Check for ^C signal. */
  201.  
  202. #define BreakCheck() (SetSignal(0,0) & SIGBREAKF_CTRL_C)
  203.  
  204.     /* Macro expressions to set the pointer for our window. */
  205.  
  206. #define SetPoint(wind)    SetPointer(wind,PointData,7,16,-4,-3)
  207. #define SetSnooze(wind)    SetPointer(wind,Snooze,22,16,-9,-13)
  208. #define SetSize(wind)    SetPointer(wind,SizeData,13,16,-4,-3)
  209.  
  210.     /* Sprite dump for the pointer image. */
  211.  
  212. USHORT PointData[(2 + 7) * 2] =
  213. {
  214.     0x0000,0x0000,
  215.  
  216.     0x0000,0x1000,
  217.     0x1000,0x0000,
  218.     0x1000,0x1000,
  219.     0x6C00,0xAA00,
  220.     0x1000,0x1000,
  221.     0x1000,0x0000,
  222.     0x0000,0x1000,
  223.  
  224.     0x0000,0x0000
  225. };
  226.  
  227.     /* Sprite dump for the Workbench balloon. */
  228.  
  229. USHORT Snooze[(2 + 22) * 2] =
  230. {
  231.     0x0000,0x0000,
  232.  
  233.     0x0700,0x0000,
  234.     0x0FA0,0x0700,
  235.     0x3FF0,0x0FA0,
  236.     0x70F8,0x3FF0,
  237.     0x7DFC,0x3FF8,
  238.     0xFBFC,0x7FF8,
  239.     0x70FC,0x3FF8,
  240.     0x7FFE,0x3FFC,
  241.     0x7F0E,0x3FFC,
  242.     0x3FDF,0x1FFE,
  243.     0x7FBE,0x3FFC,
  244.     0x3F0E,0x1FFC,
  245.     0x1FFC,0x07F8,
  246.     0x07F8,0x01E0,
  247.     0x01E0,0x0080,
  248.     0x07C0,0x0340,
  249.     0x0FE0,0x07C0,
  250.     0x0740,0x0200,
  251.     0x0000,0x0000,
  252.     0x0070,0x0020,
  253.     0x0078,0x0038,
  254.     0x0038,0x0010,
  255.  
  256.     0x0000,0x0000
  257. };
  258.  
  259. USHORT SizeData[(2 + 13) * 2] =
  260. {
  261.     0x0000,0x0000,
  262.  
  263.     0x0000,0x1000,
  264.     0x1000,0x0000,
  265.     0x1000,0x1000,
  266.     0x6C00,0xAA00,
  267.     0x1000,0x1000,
  268.     0x1000,0x0000,
  269.     0x0000,0x1000,
  270.     0x0000,0x0000,
  271.     0x0D77,0x0D77,
  272.     0x1114,0x1114,
  273.     0x1D27,0x1D27,
  274.     0x0544,0x0544,
  275.     0x1977,0x1977,
  276.  
  277.     0x0000,0x0000
  278. };
  279.  
  280.     /* This is where the colours go when the About item is
  281.      * selected.
  282.      */
  283.  
  284. UWORD            PrefColours[32];
  285. struct Preferences    StandardPrefs;
  286.  
  287.     /* MakeID(IDString) :
  288.      *
  289.      *    Transforms a string into a chunk ID. How can we do this
  290.      *    in a macro expression? Just don't know.
  291.      */
  292.  
  293. ULONG
  294. MakeID(char *IDString)
  295. {
  296.     ULONG LongID;
  297.  
  298.     LongID = (IDString[0] << 24) | (IDString[1] << 16) | (IDString[2] << 8) | (IDString[3]);
  299.  
  300.     return(LongID);
  301. }
  302.  
  303.     /* FindChunk(ChunkName,FilePointer) :
  304.      *
  305.      *    Will try to find a chunk ID somewhere in the
  306.      *    file. If it doesn't find any it returns FALSE.
  307.      *    This routine was somewhat inspired by the
  308.      *    Chunk-reader to be found on the disk distributed
  309.      *    along with the FutureSound sound sampling hardware.
  310.      *    Some months later I through the 'crap' out. Instead
  311.      *    of years this will take you only some moments to
  312.      *    load now.
  313.      */
  314.  
  315. LONG
  316. FindChunk(char *ChunkName,FILE *FilePointer)
  317. {
  318.     IFF_Chunk     Chunk;
  319.     LONG         OldPosition,FormType,WeWant;
  320.  
  321.         /* Set up the chunk type. */
  322.  
  323.     WeWant = MakeID(ChunkName);
  324.  
  325.         /* Remember the initial file position. */
  326.  
  327.     OldPosition = ftell(FilePointer);
  328.  
  329.         /* Reset the form type. */
  330.  
  331.     FormType = 0;
  332.  
  333.     for(;;)
  334.     {
  335.             /* Try to read the chunk. */
  336.  
  337.         if(fread(&Chunk,sizeof(Chunk),1,FilePointer) != 1)
  338.         {
  339.                 /* If it went wrong, reset the
  340.                  * file position.
  341.                  */
  342.  
  343.             fseek(FilePointer,OldPosition,0);
  344.             return(0);
  345.         }
  346.  
  347.             /* If this is supposed to be a FORM chunk,
  348.              * try to figure out the form type.
  349.              */
  350.  
  351.         if(OldPosition == 0 && FormType == 0 && Chunk . IFF_Type == MakeID("FORM"))
  352.         {
  353.             fread(&FormType,sizeof(LONG),1,FilePointer);
  354.  
  355.                 /* Is it the type we want? */
  356.  
  357.             if(FormType == WeWant)
  358.                 return(Chunk . IFF_Length);
  359.  
  360.             continue;
  361.         }
  362.  
  363.             /* Is this what we want? */
  364.  
  365.         if(Chunk . IFF_Type == WeWant)
  366.             return(Chunk . IFF_Length);
  367.  
  368.             /* Else, skip the length information. */
  369.  
  370.         fseek(FilePointer,Chunk . IFF_Length,1);
  371.     }
  372. }
  373.  
  374.     /* LoadHeader(FileName,BMHeader) :
  375.      *
  376.      *    Does two jobs for us: Initializes the BitMapHeader
  377.      *    and tries to figure out the Amiga ViewModes this
  378.      *    file needs. Returns -1 on failure, not NULL.
  379.      */
  380.  
  381. LONG
  382. LoadHeader(char *FileName,BitMapHeader *BMHeader)
  383. {
  384.     FILE    *ImageFile;
  385.     LONG     ViewModes = NULL;
  386.  
  387.         /* No such file? */
  388.  
  389.     if(!(ImageFile = fopen(FileName,"r")))
  390.         return(-1);
  391.  
  392.         /* No BMHD-Chunk? */
  393.  
  394.     if(!FindChunk("BMHD",ImageFile))
  395.     {
  396.         fclose(ImageFile);
  397.         return(-1);
  398.     }
  399.  
  400.         /* Read the header. */
  401.  
  402.     fread(BMHeader,sizeof(BitMapHeader),1,ImageFile);
  403.  
  404.         /* Strange values, probably not a picture but a
  405.          * "mistake", or even a CMAP.
  406.          */
  407.  
  408.     if(BMHeader -> nPlanes < 1 || BMHeader -> nPlanes > 8)
  409.     {
  410.         fclose(ImageFile);
  411.         return(-1);
  412.     }
  413.  
  414.         /* If we don't find a CAMG chunk in the file
  415.          * we will have to guess the right
  416.          * ViewModes. This line takes care of the
  417.          * interlaced display mode.
  418.          */
  419.  
  420.     if(BMHeader -> pageHeight > GfxBase -> NormalDisplayRows)
  421.         ViewModes |= LACE;
  422.  
  423.         /* Could it be HIRES? */
  424.  
  425.     if(BMHeader -> pageWidth >= 640)
  426.         ViewModes |= HIRES;
  427.  
  428.         /* It is still much more likely to encounter a
  429.          * HAM picture than an EHB picture. If we are
  430.          * wrong with this assumption, the CAMG chunk
  431.          * will tell us (hope so).
  432.          */
  433.  
  434.     if(BMHeader -> nPlanes == 6)
  435.         ViewModes |= HAM;
  436.  
  437.         /* Hello out there, got any CAMG chunk? */
  438.  
  439.     if(!FindChunk("CAMG",ImageFile))
  440.     {
  441.         fclose(ImageFile);
  442.         return(ViewModes);
  443.     }
  444.  
  445.         /* Read it then. */
  446.  
  447.     fread(&ViewModes,sizeof(LONG),1,ImageFile);
  448.  
  449.         /* Mask out all unwanted bits (thanks, Carolyn!). */
  450.  
  451.     ViewModes &= (~(SPRITES | VP_HIDE | GENLOCK_AUDIO | GENLOCK_VIDEO) | 0xFFFF);
  452.  
  453.         /* Finish it. */
  454.  
  455.     fclose(ImageFile);
  456.  
  457.     return(ViewModes);
  458. }
  459.  
  460.     /* LoadCMAP(FileName,ColourMap,MaxCol,BMHeader) :
  461.      *
  462.      *    Will load an unsigned word array with the
  463.      *    colours to be found in the CMAP chunk.
  464.      */
  465.  
  466. LONG
  467. LoadCMAP(char *FileName,UWORD *ColourMap,LONG MaxCol,BitMapHeader *BMHeader)
  468. {
  469.     LONG  i;
  470.     FILE *ColFile;
  471.     LONG  R,G,B;
  472.  
  473.         /* Are you there? */
  474.  
  475.     if(!(ColFile = fopen(FileName,"r")))
  476.         return(0);
  477.  
  478.         /* Black 'n white or colour TV? */
  479.  
  480.     if(!FindChunk("CMAP",ColFile))
  481.     {
  482.         fclose(ColFile);
  483.         return(0);
  484.     }
  485.  
  486.         /* Correct it before the reader believes it! */
  487.  
  488.     if(MaxCol < 2)
  489.         MaxCol = 1 << BMHeader -> nPlanes;
  490.  
  491.         /* A bit too large, innit? */
  492.  
  493.     if(MaxCol > 32)
  494.         MaxCol = 32;
  495.  
  496.         /* Read those colours. */
  497.  
  498.     for(i = 0 ; i < MaxCol ; i++)
  499.     {
  500.         R = fgetc(ColFile) >> 4;
  501.         G = fgetc(ColFile) >> 4;
  502.         B = fgetc(ColFile) >> 4;
  503.  
  504.             /* The transformation. */
  505.  
  506.         ColourMap[i] = (R << 8) | (G << 4) | (B);
  507.     }
  508.  
  509.         /* Finish it. */
  510.  
  511.     fclose(ColFile);
  512.  
  513.     return(MaxCol);
  514. }
  515.  
  516.     /* LoadRaster(FileName,BitPlanes,BMHeader) :
  517.      *
  518.      *    Will decompress the interleaved bitmap data
  519.      *    into a set of bitplanes.
  520.      */
  521.  
  522. UBYTE
  523. LoadRaster(char *FileName,PLANEPTR *BitPlanes,BitMapHeader *BMHeader)
  524. {
  525.     LONG         i,j,k;
  526.     UBYTE         Value,SoFar,Compr,Depth;
  527.     BYTE         ChkVal;
  528.     LONG         Height,Width;
  529.     PLANEPTR     Planes[9];    /* 9 for possible bitmask. */
  530.     FILE        *PicFile;
  531.  
  532.     BYTE        *AuxBuff;    /* Decompress in memory buffer. */
  533.     BYTE        *AuxBuff2;
  534.     LONG         AuxLength;
  535.  
  536.         /* Clear the planes. */
  537.  
  538.     for(i = 0 ; i < 9 ; i++)
  539.         Planes[i] = NULL;
  540.  
  541.         /* Set up the working copies. */
  542.  
  543.     Width    = LineBytes(BMHeader -> w);
  544.     Height    = BMHeader -> h;
  545.     Depth    = BMHeader -> nPlanes;
  546.     Compr    = BMHeader -> compression;
  547.  
  548.         /* Is there something wrong in paradise? */
  549.  
  550.     if(Compr > 1 || !BitPlanes)
  551.         return(FALSE);
  552.  
  553.         /* Can we read it, please? */
  554.  
  555.     if(!(PicFile = fopen(FileName,"r")))
  556.         return(FALSE);
  557.  
  558.         /* No BODY? What is this? */
  559.  
  560.     if(!(AuxLength = FindChunk("BODY",PicFile)))
  561.     {
  562.         fclose(PicFile);
  563.         return(FALSE);
  564.     }
  565.  
  566.         /* Copy the bitmap pointers since their
  567.          * contents will get changed.
  568.          */
  569.  
  570.     for(i = 0 ; i < Depth ; i++)
  571.         Planes[i] = BitPlanes[i];
  572.  
  573.         /* Very well, nobody told me that DPaint and Aegis Images
  574.          * are allowed to save their own home-brewn BODY chunks
  575.          * if the transparent colour is nonzero or the
  576.          * stencil/behind function is used. In this case the
  577.          * interleaved plane data is immediately followed by
  578.          * a bitmask which is to clear out all unwanted pixels
  579.          * after the image is drawn. To support this attitude
  580.          * we increment the depth of the image to give the
  581.          * reader access to a blank pointer the bitmask will
  582.          * be sent to.
  583.          */
  584.  
  585.     if(BMHeader -> masking == 1)
  586.         Depth++;
  587.  
  588.         /* If we can allocate the memory buffer, we will
  589.          * decompress the image in memory rather than
  590.          * while reading it from disk.
  591.          */
  592.  
  593.     if(AuxBuff = (BYTE *)AllocMem(AuxLength,MEMF_PUBLIC))
  594.     {
  595.             /* Read the data. */
  596.  
  597.         fread(AuxBuff,AuxLength,1,PicFile);
  598.  
  599.             /* Remember the buffer address. */
  600.  
  601.         AuxBuff2 = AuxBuff;
  602.  
  603.             /* No compression? */
  604.  
  605.         if(Compr == 0)
  606.         {
  607.             for(k = 0 ; k < Height ; k++)
  608.             {
  609.                 for(j = 0 ; j < Depth ; j++)
  610.                 {
  611.                     if(Planes[j])
  612.                     {
  613.                         CopyMem(AuxBuff,Planes[j],Width);
  614.                         Planes[j] += Width;
  615.                     }
  616.  
  617.                     AuxBuff += Width;
  618.                 }
  619.             }
  620.         }
  621.  
  622.             /* ByteRun compression? */
  623.  
  624.         if(Compr == 1)
  625.         {
  626.             for(k = 0 ; k < Height ; k++)
  627.             {
  628.                 for(j = 0 ; j < Depth ; j++)
  629.                 {
  630.                     for(SoFar = 0 ; SoFar < Width ; )
  631.                     {
  632.                         ChkVal = *AuxBuff;
  633.                         AuxBuff++;
  634.  
  635.                         if(ChkVal > 0)
  636.                         {
  637.                             if(Planes[j])
  638.                             {
  639.                                 CopyMem(AuxBuff,Planes[j],ChkVal + 1);
  640.  
  641.                                 Planes[j] += ChkVal + 1;
  642.                             }
  643.  
  644.                             AuxBuff += ChkVal + 1;
  645.  
  646.                             SoFar += ChkVal + 1;
  647.                         }
  648.                         else
  649.                         {
  650.                             if(ChkVal != -128)
  651.                             {
  652.                                 Value = *AuxBuff;
  653.                                 AuxBuff++;
  654.  
  655.                                 for(i = 0 ; i <= -ChkVal ; i++)
  656.                                 {
  657.                                     if(Planes[j])
  658.                                     {
  659.                                         *Planes[j] = Value;
  660.                                         Planes[j]++;
  661.                                     }
  662.  
  663.                                     SoFar++;
  664.                                 }
  665.                             }
  666.                         }
  667.                     }
  668.                 }
  669.             }
  670.         }
  671.  
  672.             /* Free the auxilary buffer. */
  673.  
  674.         FreeMem(AuxBuff2,AuxLength);
  675.  
  676.         goto Quit;
  677.     }
  678.         /* No compression, take the data as is. */
  679.  
  680.     if(Compr == 0)
  681.     {
  682.         for(k = 0 ; k < Height ; k++)
  683.         {
  684.             for(j = 0 ; j < Depth ; j++)
  685.             {
  686.                 if(Planes[j])
  687.                 {
  688.                     fread(Planes[j],Width,1,PicFile);
  689.                     Planes[j] += Width;
  690.                 }
  691.                 else
  692.                     fseek(PicFile,Width,1);
  693.             }
  694.         }
  695.     }
  696.  
  697.         /* ByteRun1 compression, efficient but tricky. */
  698.  
  699.     if(Compr == 1)
  700.     {
  701.         for(k = 0 ; k < Height ; k++)
  702.         {
  703.             for(j = 0 ; j < Depth ; j++)
  704.             {
  705.                 for(SoFar = 0 ; SoFar < Width ; )
  706.                 {
  707.                     ChkVal = fgetc(PicFile);
  708.  
  709.                         /* Read the next bytes. */
  710.  
  711.                     if(ChkVal > 0)
  712.                     {
  713.                         if(Planes[j])
  714.                         {
  715.                             fread(Planes[j],ChkVal + 1,1,PicFile);
  716.  
  717.                             Planes[j] += ChkVal + 1;
  718.                         }
  719.                         else
  720.                             fseek(PicFile,ChkVal + 1,1);
  721.  
  722.                         SoFar += ChkVal + 1;
  723.                     }
  724.                     else
  725.                     {
  726.                             /* Set the memory to this
  727.                              * value.
  728.                              */
  729.  
  730.                         if(ChkVal != -128)
  731.                         {
  732.                             Value = fgetc(PicFile);
  733.  
  734.                             for(i = 0 ; i <= -ChkVal ; i++)
  735.                             {
  736.                                 if(Planes[j])
  737.                                 {
  738.                                     *Planes[j] = Value;
  739.                                     Planes[j]++;
  740.                                 }
  741.  
  742.                                 SoFar++;
  743.                             }
  744.                         }
  745.                     }
  746.                 }
  747.             }
  748.         }
  749.     }
  750.  
  751.         /* Finish it up. */
  752.  
  753. Quit:    fclose(PicFile);
  754.  
  755.     return(TRUE);
  756. }
  757.  
  758.     /* FadeTo():
  759.      *
  760.      *    Fades a palette into a different palette or colour.
  761.      */
  762.  
  763. VOID
  764. FadeTo(struct ViewPort *VPort,UWORD *From,UWORD *To,LONG NumColours,LONG ToCol,LONG FromCol)
  765. {
  766.     UWORD FromTemp[32];
  767.     UWORD ToTemp[32];
  768.  
  769.     SHORT i,j,R1,G1,B1,R2,G2,B2;
  770.  
  771.         /* A bit too large? */
  772.  
  773.     if(NumColours > 32)
  774.         NumColours = 32;
  775.  
  776.         /* Do we have a source palette? */
  777.  
  778.     if(From)
  779.         CopyMem(&From[0],&FromTemp[0],sizeof(UWORD) * 32);
  780.     else
  781.         memset(&FromTemp[0],FromCol,sizeof(UWORD) * 32);
  782.  
  783.         /* Do we have a destination palette? */
  784.  
  785.     if(To)
  786.         CopyMem(&To[0],&ToTemp[0],sizeof(UWORD) * 32);
  787.     else
  788.         memset(&ToTemp[0],ToCol,sizeof(UWORD) * 32);
  789.  
  790.         /* Let's get into action. */
  791.  
  792.     for(j = 0 ; j < 16 ; j++)
  793.     {
  794.         for(i = 0 ; i < NumColours ; i++)
  795.         {
  796.                 /* Split the source colour. */
  797.  
  798.             R1 = (FromTemp[i] >> 8)    & 0xF;
  799.             G1 = (FromTemp[i] >> 4)    & 0xF;
  800.             B1 = (FromTemp[i])    & 0xF;
  801.  
  802.                 /* Split the destination colour. */
  803.  
  804.             R2 = (ToTemp[i] >> 8)    & 0xF;
  805.             G2 = (ToTemp[i] >> 4)    & 0xF;
  806.             B2 = (ToTemp[i])    & 0xF;
  807.  
  808.                 /* Adjust the colours. */
  809.  
  810.             if(R1 > R2)
  811.                 R1--;
  812.  
  813.             if(G1 > G2)
  814.                 G1--;
  815.  
  816.             if(B1 > B2)
  817.                 B1--;
  818.  
  819.                 /* Adjust the colours. */
  820.  
  821.             if(R1 < R2)
  822.                 R1++;
  823.  
  824.             if(G1 < G2)
  825.                 G1++;
  826.  
  827.             if(B1 < B2)
  828.                 B1++;
  829.  
  830.                 /* Put it together again. */
  831.  
  832.             FromTemp[i] = (R1 << 8) | (G1 << 4) | B1;
  833.         }
  834.  
  835.             /* Wait a bit. */
  836.  
  837.         Delay(2);
  838.  
  839.             /* Load the new map. */
  840.  
  841.         LoadRGB4(VPort,FromTemp,NumColours);
  842.     }
  843. }
  844.  
  845.     /* InitTinyBitMap() :
  846.      *
  847.      *    If the picture to be loaded is smaller than the
  848.      *    screen it is to appear on, a custom bitmap is
  849.      *    initialized to receive the image data.
  850.      */
  851.  
  852. UBYTE
  853. InitTinyBitMap()
  854. {
  855.     SHORT i;
  856.  
  857.         /* So the Blitter won't be confused. */
  858.  
  859.     InitBitMap(&TinyBitMap,InfoHeader . nPlanes,InfoHeader . w,InfoHeader . h);
  860.  
  861.         /* Try to steal some memory for the tiny bitmap. */
  862.  
  863.     for(i = 0 ; i < InfoHeader . nPlanes ; i++)
  864.         if(!(TinyBitMap . Planes[i] = (PLANEPTR)AllocRaster(InfoHeader . w,InfoHeader . h)))
  865.             return(FALSE);
  866.  
  867.     InitBitMap(&ScreenMap,InfoHeader . nPlanes,NewScreen . Width,NewScreen . Height);
  868.  
  869.         /* Try to steal some memory for the bitplanes. */
  870.  
  871.     for(i = 0 ; i < InfoHeader . nPlanes ; i++)
  872.         if(!(ScreenMap . Planes[i] = (PLANEPTR)AllocRaster(NewScreen . Width,NewScreen . Height)))
  873.             return(FALSE);
  874.  
  875.     return(TRUE);
  876. }
  877.  
  878.     /* CleanExit() :
  879.      *
  880.      *    Closes the libraries and exits.
  881.      */
  882.  
  883. VOID
  884. CleanExit(LONG ExitCode)
  885. {
  886.         /* Take care of external signals. */
  887.  
  888.     SetSignal(0,SIGBREAKF_CTRL_C);
  889.  
  890.         /* Return the control to DOS. See what it's doing with
  891.          * it.
  892.          */
  893.  
  894.     exit(ExitCode);
  895. }
  896.  
  897.     /* CloseDisplay() :
  898.      *
  899.      *    Closes anything that we have opened but the
  900.      *    libraries.
  901.      */
  902.  
  903. VOID
  904. CloseDisplay()
  905. {
  906.     SHORT i;
  907.  
  908.         /* Canst thou hear me? Finish what you have started. */
  909.  
  910.     ClearCycleCode();
  911.  
  912.         /* A window! */
  913.  
  914.     if(Window)
  915.     {
  916.             /* Hide the menu. */
  917.  
  918.         Window -> Flags |= RMBTRAP;
  919.  
  920.             /* We don't need the menu any more. */
  921.  
  922.         ClearMenuStrip(Window);
  923.  
  924.         CloseWindow(Window);
  925.  
  926.         Window = NULL;
  927.     }
  928.  
  929.         /* Is there a screen anywhere? */
  930.  
  931.     if(Screen)
  932.     {
  933.         CloseScreen(Screen);
  934.  
  935.         Screen = NULL;
  936.     }
  937.  
  938.         /* Get rid of the planes. */
  939.  
  940.     for(i = 0 ; i < ScreenMap . Depth ; i++)
  941.     {
  942.         if(TinyBitMap . Planes[i])
  943.         {
  944.             FreeRaster(TinyBitMap . Planes[i],InfoHeader . w,InfoHeader . h);
  945.             TinyBitMap . Planes[i] = NULL;
  946.  
  947.             if(ScreenMap . Planes[i])
  948.             {
  949.                 FreeRaster(ScreenMap . Planes[i],NewScreen . Width,NewScreen . Height);
  950.                 ScreenMap . Planes[i] = NULL;
  951.             }
  952.  
  953.             continue;
  954.         }
  955.  
  956.         if(ScreenMap . Planes[i])
  957.         {
  958.             FreeRaster(ScreenMap . Planes[i],InfoHeader . w,InfoHeader . h);
  959.             ScreenMap . Planes[i] = NULL;
  960.         }
  961.     }
  962. }
  963.  
  964.     /* PrintScreen(Large):
  965.      *
  966.      *    Sends the current LoadImage screen to the printer.
  967.      */
  968.  
  969. VOID
  970. PrintScreen(UBYTE Large)
  971. {
  972.     struct IODRPReq        *PrinterDump;
  973.     struct MsgPort        *PrinterPort;
  974.  
  975.     struct IntuiMessage    *Massage;
  976.  
  977.     ULONG             Class;
  978.     USHORT             Code;
  979.  
  980.     UBYTE             HasTitle    = (Screen -> Flags & SHOWTITLE) ? TRUE : FALSE;
  981.     UBYTE             Cycling    = IsCycling();
  982.  
  983.     LONG             StartX,StartY,Width,Height;
  984.  
  985.     volatile LONG         OldX = -1,OldY = -1,TempX,TempY,X,Y;
  986.  
  987.     SetSnooze(Window);
  988.  
  989.         /* IO Replyport. */
  990.  
  991.     if(PrinterPort = (struct MsgPort *)CreatePort(NULL,0))
  992.     {
  993.             /* Custom RastPort dump structure. */
  994.  
  995.         if(PrinterDump = (struct IODRPReq *)CreateExtIO(PrinterPort,sizeof(struct IODRPReq)))
  996.         {
  997.                 /* Try to open the device. */
  998.  
  999.             if(!OpenDevice("printer.device",0,PrinterDump,0))
  1000.             {
  1001.                 SetSize(Window);
  1002.                 SetDrMd(Window -> RPort,COMPLEMENT);
  1003.  
  1004.                     /* Don't let anyone disturb the
  1005.                      * LoadImage screen bitmap.
  1006.                      */
  1007.  
  1008.                 Window -> Flags |= RMBTRAP;
  1009.  
  1010.                 if(HasTitle)
  1011.                     ShowTitle(Screen,FALSE);
  1012.  
  1013.                 if(Cycling)
  1014.                     ToggleCycleCode();
  1015.  
  1016.                     /* Let the user set the initial
  1017.                      * point.
  1018.                      */
  1019.  
  1020.                 FOREVER
  1021.                 {
  1022.                     WaitPort(Window -> UserPort);
  1023.  
  1024.                     if(Massage = (struct IntuiMessage *)GetMsg(Window -> UserPort))
  1025.                     {
  1026.                         Class    = Massage -> Class;
  1027.                         Code    = Massage -> Code;
  1028.  
  1029.                         ReplyMsg(Massage);
  1030.                     }
  1031.  
  1032.                     if((Class == MOUSEBUTTONS && Code == MENUDOWN) || Class == RAWKEY)
  1033.                     {
  1034.                         DisplayBeep(Screen);
  1035.  
  1036.                         goto HitAndQuit;
  1037.                     }
  1038.  
  1039.                     if(Class == MOUSEBUTTONS && Code == SELECTDOWN)
  1040.                     {
  1041.                         X = Window -> MouseX;
  1042.                         Y = Window -> MouseY;
  1043.  
  1044.                         break;
  1045.                     }
  1046.                 }
  1047.  
  1048.                     /* Now let him resize the rubber box
  1049.                      * to the right dimensions.
  1050.                      */
  1051.  
  1052.                 ReportMouse(TRUE,Window);
  1053.  
  1054.                 FOREVER
  1055.                 {
  1056.                     USHORT Qualifier;
  1057.  
  1058.                     TempX = Window -> MouseX;
  1059.                     TempY = Window -> MouseY;
  1060.  
  1061.                     if(TempX < 0)
  1062.                         TempX = 0;
  1063.  
  1064.                     if(TempY < 0)
  1065.                         TempY = 0;
  1066.  
  1067.                     if(TempX > Window -> Width - 1)
  1068.                         TempX = Window -> Width - 1;
  1069.  
  1070.                     if(OldX >= 0 && OldY >= 0)
  1071.                     {
  1072.                         Move(Window -> RPort,X,Y);
  1073.                         Draw(Window -> RPort,OldX,Y);
  1074.                         Draw(Window -> RPort,OldX,OldY);
  1075.                         Draw(Window -> RPort,X,OldY);
  1076.                         Draw(Window -> RPort,X,(OldY >= Y) ? Y + 1 : Y - 1);
  1077.                     }
  1078.                     else
  1079.                         OldX = OldY = 0;
  1080.  
  1081.                     Move(Window -> RPort,X,Y);
  1082.                     Draw(Window -> RPort,TempX,Y);
  1083.                     Draw(Window -> RPort,TempX,TempY);
  1084.                     Draw(Window -> RPort,X,TempY);
  1085.                     Draw(Window -> RPort,X,(TempY >= Y) ? Y + 1 : Y - 1);
  1086.  
  1087.                     OldX = TempX;
  1088.                     OldY = TempY;
  1089.  
  1090. WaitLoop:                WaitPort(Window -> UserPort);
  1091.  
  1092.                     if(Massage = (struct IntuiMessage *)GetMsg(Window -> UserPort))
  1093.                     {
  1094.                         Class        = Massage -> Class;
  1095.                         Code        = Massage -> Code;
  1096.                         Qualifier    = Massage -> Qualifier;
  1097.  
  1098.                         ReplyMsg(Massage);
  1099.                     }
  1100.  
  1101.                     if((Class == MOUSEBUTTONS && Code == MENUDOWN) || Class == RAWKEY)
  1102.                     {
  1103.                         DisplayBeep(Screen);
  1104.  
  1105.                         Move(Window -> RPort,X,Y);
  1106.                         Draw(Window -> RPort,TempX,Y);
  1107.                         Draw(Window -> RPort,TempX,TempY);
  1108.                         Draw(Window -> RPort,X,TempY);
  1109.                         Draw(Window -> RPort,X,(TempY >= Y) ? Y + 1 : Y - 1);
  1110.  
  1111.                         goto HitAndQuit;
  1112.                     }
  1113.  
  1114.                     if(Class == MOUSEBUTTONS && Code == SELECTUP)
  1115.                     {
  1116.                         Move(Window -> RPort,X,Y);
  1117.                         Draw(Window -> RPort,TempX,Y);
  1118.                         Draw(Window -> RPort,TempX,TempY);
  1119.                         Draw(Window -> RPort,X,TempY);
  1120.                         Draw(Window -> RPort,X,(TempY >= Y) ? Y + 1 : Y - 1);
  1121.  
  1122.                         StartX    = (X < TempX) ? X : TempX;
  1123.                         StartY    = (Y < TempY) ? Y : TempY;
  1124.  
  1125.                         Width    = ABS(TempX - X) + 1;
  1126.                         Height    = ABS(TempY - Y) + 1;
  1127.  
  1128.                         if(Width < 2 || Height < 2)
  1129.                             goto HitAndQuit;
  1130.  
  1131.                         break;
  1132.                     }
  1133.                 }
  1134.  
  1135.                     /* Initialize the IO Request for
  1136.                      * a RastPort dump.
  1137.                      */
  1138.  
  1139.                 PrinterDump -> io_Command    = PRD_DUMPRPORT;
  1140.                 PrinterDump -> io_RastPort    = &Screen -> RastPort;
  1141.                 PrinterDump -> io_ColorMap    = Screen -> ViewPort . ColorMap;
  1142.                 PrinterDump -> io_Modes        = Screen -> ViewPort . Modes;
  1143.                 PrinterDump -> io_SrcWidth    = Width;
  1144.                 PrinterDump -> io_SrcHeight    = Height;
  1145.                 PrinterDump -> io_SrcX        = StartX;
  1146.                 PrinterDump -> io_SrcY        = StartY;
  1147.  
  1148.                     /* Print it as large as possible? */
  1149.  
  1150.                 if(Large)
  1151.                 {
  1152.                     PrinterDump -> io_DestCols    = 0;
  1153.                     PrinterDump -> io_Special    = SPECIAL_FULLCOLS | SPECIAL_ASPECT;
  1154.                 }
  1155.                 else
  1156.                 {
  1157.                     PrinterDump -> io_DestCols    = Width;
  1158.                     PrinterDump -> io_Special    = SPECIAL_MILCOLS | SPECIAL_ASPECT;
  1159.                 }
  1160.  
  1161.                 SetSnooze(Window);
  1162.  
  1163.                     /* Try the dump. */
  1164.  
  1165.                 if(DoIO(PrinterDump))
  1166.                     DisplayBeep(Screen);
  1167.  
  1168.                     /* Re-enable the window. */
  1169.  
  1170. HitAndQuit:            Window -> Flags &= ~RMBTRAP;
  1171.  
  1172.                 SetPoint(Window);
  1173.  
  1174.                     /* If set, re-enable the screen title. */
  1175.  
  1176.                 if(HasTitle)
  1177.                     ShowTitle(Screen,TRUE);
  1178.  
  1179.                 if(Cycling)
  1180.                     ToggleCycleCode();
  1181.  
  1182.                 ReportMouse(FALSE,Window);
  1183.  
  1184.                 CloseDevice(PrinterDump);
  1185.             }
  1186.             else
  1187.                 DisplayBeep(Screen);
  1188.  
  1189.             DeleteExtIO(PrinterDump);
  1190.         }
  1191.         else
  1192.             DisplayBeep(Screen);
  1193.  
  1194.         DeletePort(PrinterPort);
  1195.     }
  1196.     else
  1197.         DisplayBeep(Screen);
  1198.  
  1199.     SetPoint(Window);
  1200. }
  1201.  
  1202.     /* GetRealName():
  1203.      *
  1204.      *    Looks for a file and returns its real name.
  1205.      */
  1206.  
  1207. VOID
  1208. GetRealName(char *FullName,char *RealName)
  1209. {
  1210.     struct FileInfoBlock    *FileInfo;
  1211.     BPTR             FileLock;
  1212.  
  1213.     if(FileLock = Lock(FullName,ACCESS_READ))
  1214.     {
  1215.         if(FileInfo = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),MEMF_PUBLIC))
  1216.         {
  1217.             if(Examine(FileLock,FileInfo))
  1218.                 strcpy(RealName,FileInfo -> fib_FileName);
  1219.                 
  1220.             FreeMem(FileInfo,sizeof(struct FileInfoBlock));
  1221.         }
  1222.  
  1223.         UnLock(FileLock);
  1224.     }
  1225. }
  1226.  
  1227.     /* LoadImage():
  1228.      *
  1229.      *    Will finally load and display an image file.
  1230.      */
  1231.  
  1232. LONG
  1233. LoadImage(char *FileName,UBYTE ForceScroll,UBYTE ForceLace,UBYTE CycleOnStartup,UBYTE LastOne)
  1234. {
  1235.     UWORD             Colours[32];        /* Colour buffer (yes I'm British!) */
  1236.     LONG             ViewModes;
  1237.     WORD             ColourNumber;        /* Number of colours. */
  1238.  
  1239.     SHORT             MaxOffsetX,MaxOffsetY;    /* Some scrolling stuff. */
  1240.     volatile SHORT         StartOffsetX,StartOffsetY;
  1241.  
  1242.     SHORT             Width,Height;        /* Size of the ViewPort */
  1243.  
  1244.     SHORT             JumpY = 1,JumpX = 1;    /* Scrolljump steps */
  1245.  
  1246.     UBYTE             WeAreScrolling = FALSE;
  1247.     UBYTE             RemakeTheView;
  1248.  
  1249.     USHORT             MenuNum;
  1250.     struct MenuItem        *Item;
  1251.  
  1252.     UBYTE             IsMouse;        /* Scrollmode. */
  1253.  
  1254.     UBYTE             WasCycling;        /* What happened? */
  1255.     UBYTE             ChangedColours = FALSE;
  1256.  
  1257.     UBYTE             IsTiny = FALSE;    /* Image smaller than screen? */
  1258.     LONG             TinyX,TinyY;        /* Image offsets for tiny bitmap. */
  1259.  
  1260.     struct IntuiMessage    *Massage;
  1261.  
  1262.     ULONG             Class;
  1263.     USHORT             Code;
  1264.  
  1265.     SHORT             i;            /* Loop counters. */
  1266.  
  1267.     volatile UBYTE        *RightMouse = (UBYTE *)0xDFF016;
  1268.  
  1269.     char             RealName[108];
  1270.  
  1271.     GetRealName(FileName,RealName);
  1272.  
  1273.         /* Is it the last picture? */
  1274.  
  1275.     if(LastOne)
  1276.         MenuItem[8] . Flags &= ~ITEMENABLED;
  1277.     else
  1278.         MenuItem[8] . Flags |= ITEMENABLED;
  1279.  
  1280.         /* Try to load the header. */
  1281.  
  1282.     if((ViewModes = LoadHeader(FileName,&InfoHeader)) == -1)
  1283.         return(ERR_NOIFF);
  1284.  
  1285.         /* Are there any colours out there? */
  1286.  
  1287.     if(!(ColourNumber = LoadCMAP(FileName,Colours,32,&InfoHeader)))
  1288.         return(ERR_NOCOLMAP);
  1289.  
  1290.         /* Take care of the size of the ViewPort. */
  1291.  
  1292.     Width    = InfoHeader . pageWidth;
  1293.     Height    = InfoHeader . pageHeight;
  1294.  
  1295.         /* Forced scrolling? */
  1296.  
  1297.     if(ForceScroll)
  1298.         ViewModes &= ~(HIRES | LACE);
  1299.  
  1300.         /* Forced interlaced display mode? */
  1301.  
  1302.     if(ForceLace)
  1303.         ViewModes |= LACE;
  1304.  
  1305.         /* Take care of exotic screen sizes. */
  1306.  
  1307.     if(InfoHeader . h > Height)
  1308.         Height = InfoHeader . h;
  1309.  
  1310.     if(InfoHeader . w > Width)
  1311.         Width = InfoHeader . w;
  1312.  
  1313.         /* Hires or lores? */
  1314.  
  1315.     if(ViewModes & HIRES)
  1316.     {
  1317.         Width = 640;
  1318.         JumpX = 2;
  1319.     }
  1320.     else
  1321.         Width = 320;
  1322.  
  1323.         /* Interlaced or not? This may get funny since
  1324.          * it is possible to scroll the image in steps
  1325.          * of one pixel. In doing so one copper list
  1326.          * (either shortframe or longframe) is ignored
  1327.          * and only half of the image is displayed
  1328.          * correctly. To avoid this the scroll jump
  1329.          * is set to two pixels.
  1330.          */
  1331.  
  1332.     if(ViewModes & LACE)
  1333.         JumpY = 2;
  1334.  
  1335.     if(Height > GfxBase -> NormalDisplayRows * JumpY)
  1336.         Height = GfxBase -> NormalDisplayRows * JumpY;
  1337.  
  1338.         /* Adjust the information. */
  1339.  
  1340.     NewScreen . Width     = Width;
  1341.     NewScreen . Height    = Height;
  1342.     NewScreen . Depth    = InfoHeader . nPlanes;
  1343.     NewScreen . ViewModes    = ViewModes;
  1344.  
  1345.         /* And don't forget the window. */
  1346.  
  1347.     NewWindow . Width    = Width;
  1348.     NewWindow . Height    = Height;
  1349.  
  1350.         /* Anything wrong with the picture? */
  1351.  
  1352.     if(InfoHeader . w < InfoHeader . pageWidth || InfoHeader . h < InfoHeader . pageHeight)
  1353.     {
  1354.         if(!InitTinyBitMap())
  1355.             return(ERR_NOMEM);
  1356.  
  1357.         IsTiny = TRUE;
  1358.     }
  1359.     else
  1360.     {
  1361.             /* Initialize the bitmap for future use. */
  1362.  
  1363.         InitBitMap(&ScreenMap,InfoHeader . nPlanes,InfoHeader . w,InfoHeader . h);
  1364.  
  1365.             /* Try to steal some memory for the bitplanes. */
  1366.  
  1367.         for(i = 0 ; i < InfoHeader . nPlanes ; i++)
  1368.         {
  1369.             if(!(ScreenMap . Planes[i] = (PLANEPTR)AllocRaster(InfoHeader . w,InfoHeader . h)))
  1370.                 return(ERR_NOMEM);
  1371.  
  1372.             IsTiny = FALSE;
  1373.         }
  1374.     }
  1375.  
  1376.         /* Open the screen. */
  1377.  
  1378.     if(!(Screen = (struct Screen *)OpenScreen(&NewScreen)))
  1379.         return(ERR_NOMEM);
  1380.  
  1381.         /* Hide the title bar and prepare the window. */
  1382.  
  1383.     ShowTitle(Screen,FALSE);
  1384.  
  1385.     NewWindow . Screen = Screen;
  1386.  
  1387.         /* Try to open the window. */
  1388.  
  1389.     if(!(Window = (struct Window *)OpenWindow(&NewWindow)))
  1390.         return(ERR_NOMEM);
  1391.  
  1392.     SetSnooze(Window);
  1393.  
  1394.     SetWindowTitles(Window,(STRPTR)-1,(STRPTR)RealName);
  1395.  
  1396.         /* Set up the alternate colour palette. */
  1397.  
  1398.     for(i = 0 ; i < 32 ; i++)
  1399.     {
  1400.         PrefColours[i] = Colours[i];
  1401.  
  1402.         if(i >= (1 << NewScreen . Depth))
  1403.             PrefColours[i] = Colours[i] = GetRGB4(Screen -> ViewPort . ColorMap,i);
  1404.     }
  1405.  
  1406.     PrefColours[0] = StandardPrefs . color0;
  1407.     PrefColours[1] = StandardPrefs . color1;
  1408.     PrefColours[2] = StandardPrefs . color2;
  1409.     PrefColours[3] = StandardPrefs . color3;
  1410.  
  1411.         /* Load the colours. */
  1412.  
  1413.     LoadRGB4(&Screen -> ViewPort,Colours,ColourNumber);
  1414.  
  1415.         /* Just in case we will have to restore it. */
  1416.  
  1417.     StartOffsetX = Screen -> ViewPort . RasInfo -> RxOffset;
  1418.     StartOffsetY = Screen -> ViewPort . RasInfo -> RyOffset;
  1419.  
  1420.         /* Play it safe --- keep the maximum scroll offset
  1421.          * in reasonable dimensions.
  1422.          */
  1423.  
  1424.     if((MaxOffsetX = InfoHeader . w - Width) < 0)
  1425.         MaxOffsetX = 0;
  1426.  
  1427.     if((MaxOffsetY = InfoHeader . h - Height) < 0)
  1428.         MaxOffsetY = 0;
  1429.  
  1430.         /* Absolute maximum limit. */
  1431.  
  1432.     MaxOffsetX += StartOffsetX;
  1433.     MaxOffsetY += StartOffsetY;
  1434.  
  1435.         /* "Here comes the sun..." */
  1436.  
  1437.     if(IsTiny)
  1438.     {
  1439.         if(!LoadRaster(FileName,TinyBitMap . Planes,&InfoHeader))
  1440.             return(ERR_DOS);
  1441.     }
  1442.     else
  1443.     {
  1444.         if(!LoadRaster(FileName,ScreenMap . Planes,&InfoHeader))
  1445.             return(ERR_DOS);
  1446.     }
  1447.  
  1448.         /* Do we allow colour cycling? */
  1449.  
  1450.     if(LoadCycleRange(FileName,CycleRange,6))
  1451.         InitCycleCode(&Screen -> ViewPort,Colours,ColourNumber,CycleRange,6);
  1452.  
  1453.         /* Install the menu and the pointer. */
  1454.  
  1455.     SetMenuStrip(Window,&Menu);
  1456.     SetPoint(Window);
  1457.  
  1458.         /* And - if necessary - print the tiny image. */
  1459.  
  1460.     if(IsTiny)
  1461.     {
  1462.         if(InfoHeader . x + InfoHeader . w <= Screen -> Width)
  1463.             TinyX = InfoHeader . x;
  1464.         else
  1465.             TinyX = (NewScreen . Width - InfoHeader . w) >> 1;
  1466.  
  1467.         if(InfoHeader . y + InfoHeader . h <= Screen -> Height)
  1468.             TinyY = InfoHeader . y;
  1469.         else
  1470.             TinyY = (NewScreen . Height - InfoHeader . h) >> 1;
  1471.  
  1472.         BltBitMap(&TinyBitMap,0,0,Screen -> RastPort . BitMap,TinyX,TinyY,InfoHeader . w,InfoHeader . h,0xC0,0xFF,NULL);
  1473.     }
  1474.  
  1475.         /* Paint the screen black (for the effect). */
  1476.  
  1477.     if(!(ViewModes & HAM))
  1478.         LoadRGB4(&Screen -> ViewPort,BlackIsBlack,ColourNumber);
  1479.  
  1480.         /* Bring the screen to the front. */
  1481.  
  1482.     ScreenToFront(Screen);
  1483.     ActivateWindow(Window);
  1484.  
  1485.     FreeSprite(0);
  1486.  
  1487.         /* Fade the colours in. */
  1488.  
  1489.     if(!(ViewModes & HAM))
  1490.         FadeTo(&Screen -> ViewPort,NULL,Colours,ColourNumber,0,0);
  1491.  
  1492.         /* Allow the menu to be selected. */
  1493.  
  1494.     Window -> Flags &= ~RMBTRAP;
  1495.  
  1496.         /* If we are to cycle... */
  1497.  
  1498.     if(CycleOnStartup)
  1499.         ToggleCycleCode();
  1500.  
  1501.         /* "Ewig währt am längsten." : Kurt Schwitters (1887 - 1948).
  1502.          *
  1503.          *                             Kurt Schwitters was a famous
  1504.          *                             Hanoverian Dada artist.
  1505.          */
  1506.  
  1507.     FOREVER
  1508.     {
  1509.             /* Let's be nice and wait for reactions. */
  1510.  
  1511.         WaitPort(Window -> UserPort);
  1512.  
  1513.             /* Massage the userport. */
  1514.  
  1515.         if(Massage = (struct IntuiMessage *)GetMsg(Window -> UserPort))
  1516.         {
  1517.             Class    = Massage -> Class;
  1518.             Code    = Massage -> Code;
  1519.  
  1520.                 /* If the user presses the menu button
  1521.                  * the menu appears. There is nothing
  1522.                  * wrong with this, but it might be
  1523.                  * possible that the user does not
  1524.                  * see the menu because of the raster
  1525.                  * offset. So we reposition the ViewPort
  1526.                  * before Intuition renders the menu bar.
  1527.                  * To keep the menu visible we adjust
  1528.                  * the colours if the right mouse
  1529.                  * button is pressed. This is done
  1530.                  * because of the fact that the
  1531.                  * keyboard shortcuts generate the
  1532.                  * same MENUVERIFY event. If there is
  1533.                  * anyone who doesn't like it this
  1534.                  * way he can try to test the keyboard
  1535.                  * matrix by tickling the hardware.
  1536.                  * I have chosen the easier way.
  1537.                  */
  1538.  
  1539.             if(Class == MENUVERIFY && !(*RightMouse & 4))
  1540.             {
  1541.                 if(WasCycling = IsCycling())
  1542.                     ToggleCycleCode();
  1543.  
  1544.                 LoadRGB4(&Screen -> ViewPort,PrefColours,32);
  1545.  
  1546.                 ChangedColours = TRUE;
  1547.  
  1548.                     /* Reset the offsets. */
  1549.  
  1550.                 Screen -> ViewPort . RasInfo -> RxOffset = StartOffsetX;
  1551.                 Screen -> ViewPort . RasInfo -> RyOffset = StartOffsetY;
  1552.  
  1553.                     /* Remake the copper list. */
  1554.  
  1555.                 MakeScreen(Screen);
  1556.                 RethinkDisplay();
  1557.             }
  1558.  
  1559.             ReplyMsg(Massage);
  1560.         }
  1561.  
  1562.             /* If the user presses the select button
  1563.              * we'll assume that he wants to scroll
  1564.              * around in the picture.
  1565.              */
  1566.  
  1567.         if(Class == MOUSEBUTTONS && Code == SELECTDOWN)
  1568.         {
  1569.             FreeSprite(0);
  1570.  
  1571.             if(ChangedColours)
  1572.             {
  1573.                 LoadRGB4(&Screen -> ViewPort,Colours,ColourNumber);
  1574.                 ChangedColours = FALSE;
  1575.  
  1576.                 if(WasCycling)
  1577.                     ToggleCycleCode();
  1578.  
  1579.                 WasCycling = FALSE;
  1580.             }
  1581.  
  1582.             WeAreScrolling    = TRUE;
  1583.             IsMouse        = TRUE;
  1584.         }
  1585.  
  1586.             /* User pressed the cursor keys and wants
  1587.              * to scroll around in the picture.
  1588.              */
  1589.  
  1590.         if(Class == RAWKEY && (Code & ~IECODE_UP_PREFIX) >= CURSORUP && (Code & ~IECODE_UP_PREFIX) <= CURSORLEFT)
  1591.         {
  1592.             FreeSprite(0);
  1593.  
  1594.             WeAreScrolling    = TRUE;
  1595.             IsMouse        = FALSE;
  1596.         }
  1597.  
  1598.             /* User didn't like the menu shortcut
  1599.              * and pressed the Tab key (is he a
  1600.              * DPaint fanatic?).
  1601.              */
  1602.  
  1603.         if(Class == RAWKEY && Code == 0x42)
  1604.         {
  1605.             FreeSprite(0);
  1606.  
  1607.             ToggleCycleCode();
  1608.         }
  1609.  
  1610.             /* The user picked a menu item. */
  1611.  
  1612.         if(Class == MENUPICK)
  1613.         {
  1614.             if(ChangedColours)
  1615.             {
  1616.                 LoadRGB4(&Screen -> ViewPort,Colours,ColourNumber);
  1617.  
  1618.                 if(WasCycling)
  1619.                     ToggleCycleCode();
  1620.  
  1621.                 ChangedColours    = FALSE;
  1622.                 WasCycling    = FALSE;
  1623.             }
  1624.  
  1625.             MenuNum = Code;
  1626.  
  1627.                 /* Until the last event is traced. */
  1628.  
  1629.             while(MenuNum != MENUNULL)
  1630.             {
  1631.                 if(MENUNUM(MenuNum) == 0)
  1632.                 {
  1633.                     switch(ITEMNUM(MenuNum))
  1634.                     {
  1635.                             /* About... */
  1636.  
  1637.                         case 0:    if(WasCycling = IsCycling())
  1638.                                 ToggleCycleCode();
  1639.  
  1640.                             LoadRGB4(&Screen -> ViewPort,PrefColours,32);
  1641.  
  1642.                                 /* Reset the offsets. */
  1643.  
  1644.                             Screen -> ViewPort . RasInfo -> RxOffset = StartOffsetX;
  1645.                             Screen -> ViewPort . RasInfo -> RyOffset = StartOffsetY;
  1646.  
  1647.                                 /* Remake the copper list. */
  1648.  
  1649.                             MakeScreen(Screen);
  1650.                             RethinkDisplay();
  1651.  
  1652.                             InitRequester(&Req);
  1653.  
  1654.                             Req . Width    = 300;
  1655.                             Req . Height    = 175;
  1656.  
  1657.                             Req . LeftEdge    = (Window -> Width - Req . Width) / 2;
  1658.                             Req . TopEdge    = (Window -> Height - Req . Height) / 2;
  1659.  
  1660.                             Req . BackFill    = 1;
  1661.                             Req . Flags    = NOISYREQ | SIMPLEREQ;
  1662.  
  1663.                             Req . ReqGadget    = &ReqGad;
  1664.                             Req . ReqBorder    = &ReqBrd[2];
  1665.                             Req . ReqText    = &ReqIntTxt[1];
  1666.  
  1667.                             if(Request(&Req,Window))
  1668.                             {
  1669.                                 FOREVER
  1670.                                 {
  1671.                                     WaitPort(Window -> UserPort);
  1672.  
  1673.                                     while(Massage = (struct IntuiMessage *)GetMsg(Window -> UserPort))
  1674.                                     {
  1675.                                         Class = Massage -> Class;
  1676.  
  1677.                                         ReplyMsg(Massage);
  1678.  
  1679.                                         if(Class == GADGETUP || Class == RAWKEY)
  1680.                                         {
  1681.                                             EndRequest(&Req,Window);
  1682.                                             goto EndReq;
  1683.                                         }
  1684.                                     }
  1685.                                 }
  1686.                             }
  1687.  
  1688. EndReq:                            SetPoint(Window);
  1689.  
  1690.                             LoadRGB4(&Screen -> ViewPort,Colours,ColourNumber);
  1691.  
  1692.                             if(WasCycling)
  1693.                                 ToggleCycleCode();
  1694.  
  1695.                             FreeSprite(0);
  1696.                             break;
  1697.  
  1698.                             /* Toggle the presence of the screen title. */
  1699.  
  1700.                         case 2:    FreeSprite(0);
  1701.  
  1702.                             if(Screen -> Flags & SHOWTITLE)
  1703.                                 ShowTitle(Screen,FALSE);
  1704.                             else
  1705.                                 ShowTitle(Screen,TRUE);
  1706.  
  1707.                             break;
  1708.  
  1709.                             /* Toggle cycling. */
  1710.  
  1711.                         case 3:    FreeSprite(0);
  1712.  
  1713.                             ToggleCycleCode();
  1714.                             break;
  1715.  
  1716.                             /* Print screen (standard size). */
  1717.  
  1718.                         case 5:    Screen -> ViewPort . RasInfo -> RxOffset = StartOffsetX;
  1719.                             Screen -> ViewPort . RasInfo -> RyOffset = StartOffsetY;
  1720.  
  1721.                             /* Remake the copper list. */
  1722.  
  1723.                             MakeScreen(Screen);
  1724.                             RethinkDisplay();
  1725.  
  1726.                             PrintScreen(FALSE);
  1727.  
  1728.                             break;
  1729.  
  1730.                             /* Print screen (enlarged). */
  1731.  
  1732.                         case 6:    Screen -> ViewPort . RasInfo -> RxOffset = StartOffsetX;
  1733.                             Screen -> ViewPort . RasInfo -> RyOffset = StartOffsetY;
  1734.  
  1735.                             /* Remake the copper list. */
  1736.  
  1737.                             MakeScreen(Screen);
  1738.                             RethinkDisplay();
  1739.  
  1740.                             PrintScreen(TRUE);
  1741.  
  1742.                             break;
  1743.  
  1744.                             /* Next picture. */
  1745.  
  1746.                         case 8:    if(IsCycling())
  1747.                                 ToggleCycleCode();
  1748.  
  1749.                             WasCycling = FALSE;
  1750.  
  1751.                             if(!(ViewModes & HAM))
  1752.                                 FadeTo(&Screen -> ViewPort,Colours,NULL,ColourNumber,0,0);
  1753.  
  1754.                             ScreenToBack(Screen);
  1755.  
  1756.                             CloseDisplay();
  1757.  
  1758.                             return(0);
  1759.  
  1760.                             /* Quit LoadImage. */
  1761.  
  1762.                         case 9:    if(IsCycling())
  1763.                                 ToggleCycleCode();
  1764.  
  1765.                             WasCycling = FALSE;
  1766.  
  1767.                             if(!(ViewModes & HAM))
  1768.                                 FadeTo(&Screen -> ViewPort,Colours,NULL,ColourNumber,0,0);
  1769.  
  1770.                             ScreenToBack(Screen);
  1771.  
  1772.                             CloseDisplay();
  1773.  
  1774.                             return(-1);
  1775.                     }
  1776.                 }
  1777.  
  1778.                     /* See if there is another
  1779.                      * menu item around.
  1780.                      */
  1781.  
  1782.                 Item = (struct MenuItem *)ItemAddress(&Menu,MenuNum);
  1783.  
  1784.                 MenuNum = Item -> NextSelect;
  1785.             }
  1786.         }
  1787.  
  1788.             /* No chance to scroll anywhere, so we'll block
  1789.              * it.
  1790.              */
  1791.  
  1792.         if(MaxOffsetX == StartOffsetX && MaxOffsetY == StartOffsetY)
  1793.             WeAreScrolling = FALSE;
  1794.  
  1795.             /* This loop will run until the select button
  1796.              * or the cursor key is released.
  1797.              */
  1798.  
  1799.         while(WeAreScrolling)
  1800.         {
  1801.             if(Massage = (struct IntuiMessage *)GetMsg(Window -> UserPort))
  1802.             {
  1803.                 Class = Massage -> Class;
  1804.                 Code  = Massage -> Code;
  1805.  
  1806.                 ReplyMsg(Massage);
  1807.             }
  1808.  
  1809.                 /* User doesn't want to scroll anymore? */
  1810.  
  1811.             if(Code & IECODE_UP_PREFIX)
  1812.             {
  1813.                 FreeSprite(0);
  1814.                 WeAreScrolling = FALSE;
  1815.             }
  1816.  
  1817.                 /* Nothing happened. */
  1818.  
  1819.             RemakeTheView = FALSE;
  1820.  
  1821.                 /* Left border, scroll left. */
  1822.  
  1823.             if(((Window -> MouseX == 0 && IsMouse) || (Code == CURSORLEFT && !IsMouse)) && (Screen -> ViewPort . RasInfo -> RxOffset - JumpX >= StartOffsetX))
  1824.             {
  1825.                 Screen -> ViewPort . RasInfo -> RxOffset -= JumpX;
  1826.                 RemakeTheView = TRUE;
  1827.             }
  1828.  
  1829.                 /* Top border, scroll up. */
  1830.  
  1831.             if(((Window -> MouseY == 0 && IsMouse) || (Code == CURSORUP && !IsMouse)) && (Screen -> ViewPort . RasInfo -> RyOffset - JumpY >= StartOffsetY))
  1832.             {
  1833.                 Screen -> ViewPort . RasInfo -> RyOffset -= JumpY;
  1834.                 RemakeTheView = TRUE;
  1835.             }
  1836.  
  1837.                 /* Right border, scroll right. */
  1838.  
  1839.             if(((Window -> MouseX == Window -> Width - 1 && IsMouse) || (Code == CURSORRIGHT && !IsMouse)) && (Screen -> ViewPort . RasInfo -> RxOffset + JumpX <= MaxOffsetX))
  1840.             {
  1841.                 Screen -> ViewPort . RasInfo -> RxOffset += JumpX;
  1842.                 RemakeTheView = TRUE;
  1843.             }
  1844.  
  1845.                 /* Bottom border, scroll down. */
  1846.  
  1847.             if(((Window -> MouseY == Window -> Height - 1 && IsMouse) || (Code == CURSORDOWN && !IsMouse)) && (Screen -> ViewPort . RasInfo -> RyOffset + JumpY <= MaxOffsetY))
  1848.             {
  1849.                 Screen -> ViewPort . RasInfo -> RyOffset += JumpY;
  1850.                 RemakeTheView = TRUE;
  1851.             }
  1852.  
  1853.                 /* Are we to scroll the ViewPort? */
  1854.  
  1855.             if(RemakeTheView)
  1856.             {
  1857.                 MakeScreen(Screen);
  1858.                 RethinkDisplay();
  1859.             }
  1860.         }
  1861.     }
  1862. }
  1863.  
  1864.     /* QuickSort(av,n) :
  1865.      *
  1866.      *    Quicksort routine borrowed from Steve Drew's revision
  1867.      *    of Matt Dillon's Shell. Please don't ask me how it
  1868.      *    works, I forgot it some moments after my Pascal teacher
  1869.      *    had explained it to me in detail.
  1870.      */
  1871.  
  1872. VOID
  1873. QuickSort(char **av,LONG n)
  1874. {
  1875.     LONG b;
  1876.  
  1877.     if(n > 0)
  1878.     {
  1879.         b = QSplit(av,n);
  1880.  
  1881.         QuickSort(av,b);
  1882.         QuickSort(av + b + 1,n - b - 1);
  1883.     }
  1884. }
  1885.  
  1886.     /* QSplit(av,n) :
  1887.      *
  1888.      *    Quicksort subroutine to save stack space.
  1889.      */
  1890.  
  1891. LONG
  1892. QSplit(char **av,LONG n)
  1893. {
  1894.     LONG i,b;
  1895.     char *element,*scr;
  1896.  
  1897.     element = av[0];
  1898.  
  1899.     for(b = 0,i = 1 ; i < n ; i++)
  1900.     {
  1901.         if(Strcmp(av[i],element) < 0)
  1902.         {
  1903.             b++;
  1904.  
  1905.             scr = av[i];
  1906.             av[i] = av[b];
  1907.             av[b] = scr;
  1908.         }
  1909.     }
  1910.  
  1911.     scr = av[0];
  1912.     av[0] = av[b];
  1913.     av[b] = scr;
  1914.  
  1915.     return(b);
  1916. }
  1917.  
  1918.     /* Chk_Abort() :
  1919.      *
  1920.      *    The system calls this during I/O. Returns 0 to tell
  1921.      *    it that there is no CTRL-C around.
  1922.      */
  1923.  
  1924. LONG Chk_Abort(VOID) { return(0); }
  1925.  
  1926.     /* _wb_parse(CurrentProcess,WBMsg) :
  1927.      *
  1928.      *    Modified Workbench tool parsing routine. This one
  1929.      *    always opens a standard output window if the program
  1930.      *    has been started from Workbench. Original version
  1931.      *    Copyright (C) 1986,1987 by Manx Software Systems, Inc.
  1932.      */
  1933.  
  1934. void
  1935. _wb_parse(struct Process *CurrentProcess,struct WBStartup *WBMsg)
  1936. {
  1937.     struct FileHandle    *FileHandlePtr;        /* Window pointer. */
  1938.     BPTR             FileWindow;        /* BCPL window pointer. */
  1939.     static char         WindowTitle[40 + 18];    /* Title buffer. */
  1940.     SHORT             i = 0,j = 0;        /* Counters. */
  1941.  
  1942.     if(WBMsg -> sm_NumArgs == 1)
  1943.         return;
  1944.  
  1945.         /* Set up the window title. */
  1946.  
  1947.     strcpy(WindowTitle,"CON:0/11/640/80/");
  1948.  
  1949.         /* Find the end of the window title string. */
  1950.  
  1951.     while(WindowTitle[i])
  1952.         i++;
  1953.  
  1954.         /* Append the tool name. */
  1955.  
  1956.     while((WindowTitle[i++] = WBMsg -> sm_ArgList -> wa_Name[j++]) && j != 40);
  1957.  
  1958.         /* Window title null termination. */
  1959.  
  1960.     WindowTitle[40 + 17] = 0;       
  1961.  
  1962.         /* Open the window. */
  1963.  
  1964.     if(FileWindow = Open(WindowTitle, MODE_OLDFILE)) 
  1965.     {
  1966.             /* Convert BPTR to window pointer. */
  1967.  
  1968.         FileHandlePtr = (struct FileHandle *)BADDR(FileWindow);
  1969.  
  1970.         CurrentProcess -> pr_ConsoleTask    = (APTR)FileHandlePtr -> fh_Type;
  1971.  
  1972.             /* Set up console IO streams. */
  1973.  
  1974.         CurrentProcess -> pr_CIS        = FileWindow;
  1975.         CurrentProcess -> pr_COS        = Open("*", MODE_OLDFILE);
  1976.  
  1977.             /* Setup for exit to close these if we succeeded. */
  1978.  
  1979.         _devtab[0] . mode    &= ~O_STDIO;
  1980.         _devtab[1] . mode    &= ~O_STDIO;
  1981.     }
  1982. }
  1983.  
  1984. VOID
  1985. main(int argc,char **argv)
  1986. {
  1987.     struct FileRequester    *Request;
  1988.     SHORT             i,NumItems,Error;
  1989.     char             TempName[DSIZE * 10 + FCHARS];
  1990.  
  1991.         /* Setup the standard colours for later use. */
  1992.  
  1993.     GetPrefs(&StandardPrefs,sizeof(struct Preferences));
  1994.  
  1995.         /* Workbench startup? */
  1996.  
  1997.     if(!argc)
  1998.     {
  1999.         extern struct WBStartup    *WBenchMsg;
  2000.         struct WBArg        *WArg;
  2001.         char             FullPath[DSIZE * 10];
  2002.  
  2003.             /* Didn't select any project icons? */
  2004.  
  2005.         if((NumItems = WBenchMsg -> sm_NumArgs - 1) < 1)
  2006.         {
  2007.             if(Request = ArpAllocFreq())
  2008.             {
  2009.                 Request -> fr_Hail = "Select file to display";
  2010.  
  2011.                 FOREVER
  2012.                 {
  2013.                     if(FileRequest(Request))
  2014.                     {
  2015.                         strcpy(TempName,Request -> fr_Dir);
  2016.  
  2017.                         TackOn(TempName,Request -> fr_File);
  2018.  
  2019.                         if(LoadImage(TempName,FALSE,FALSE,FALSE,FALSE) > 0)
  2020.                             DisplayBeep(NULL);
  2021.                     }
  2022.                     else
  2023.                         break;
  2024.                 }
  2025.             }
  2026.  
  2027.             CleanExit(RETURN_OK);
  2028.         }
  2029.  
  2030.             /* Take the first argument. */
  2031.  
  2032.         WArg = WBenchMsg -> sm_ArgList;
  2033.         WArg++;
  2034.  
  2035.             /* Scan the list... */
  2036.  
  2037.         for(i = 0 ; i < NumItems ; i++)
  2038.         {
  2039.                 /* Build image file name. */
  2040.  
  2041.             PathName(WArg -> wa_Lock,FullPath,DSIZE * 10);
  2042.  
  2043.             TackOn(FullPath,WArg -> wa_Name);
  2044.  
  2045.             WArg++;
  2046.  
  2047.             Printf("Picture \33[33mNº %03ld\33[31m, \33[1m\"%s\"\33[0m loading... ",i + 1,FullPath);
  2048.  
  2049.             if((Error = LoadImage(FullPath,FALSE,FALSE,TRUE,(i == NumItems - 1))) < 0 || BreakCheck())
  2050.             {
  2051.                 if(BreakCheck())
  2052.                     Puts("*** aborted.\a");
  2053.                 else
  2054.                     Puts("");
  2055.  
  2056.                 break;
  2057.             }
  2058.  
  2059.             switch(Error)
  2060.             {
  2061.                 case ERR_NOIFF:        Printf("\33[33mNot an IFF-ILBM file.\33[31m\a");
  2062.                             break;
  2063.  
  2064.                 case ERR_NOCOLMAP:    Printf("\33[33mNo colour map.\33[31m\a");
  2065.                             break;
  2066.  
  2067.                 case ERR_NOMEM:        Printf("\33[33mNot enough memory.\33[31m\a");
  2068.                             break;
  2069.  
  2070.                 case ERR_DOS:        Printf("\33[33mError while reading.\33[31m\a");
  2071.                             break;
  2072.  
  2073.                 default:        Printf("Done.");
  2074.                             break;
  2075.             }
  2076.  
  2077.             Puts("");
  2078.         }
  2079.  
  2080.         Printf("\nPlease press \33[1mRETURN\33[0m.");
  2081.  
  2082.         Read(Input(),FullPath,1);
  2083.  
  2084.         CleanExit(RETURN_OK);
  2085.     }
  2086.     else
  2087.     {
  2088.         UBYTE     ForceScroll = FALSE,ForceLace = FALSE,Cycle = FALSE;
  2089.         SHORT     Offset = 1,NumFiles,j;
  2090.         char    *FileArray[256],*Pat;
  2091.  
  2092.             /* User wants information? */
  2093.  
  2094.         if(argv[ARG_INFO])
  2095.         {
  2096.             Printf("\n \33[1m\33[33mLoadImage\33[31m\33[0m © Copyright 1990 by MXM\n\n");
  2097.             Printf(" Usage is: \33[1mLoadImage\33[0m [INFO] [NOMODE]/[LACE] [CYCLE] Files1 ... FileN\n");
  2098.             Printf("           \33[33mAmigaDOS\33[31m wildcards are supported!\n\n");
  2099.  
  2100.             Printf("           \33[1mINFO\33[0m   \33[33m»\33[31m Displays this page.\n");
  2101.             Printf("           \33[1mNOMODE\33[0m \33[33m»\33[31m Force scrollmode (ignore Hi-Res & Interlace flags).\n");
  2102.             Printf("           \33[1mLACE\33[0m   \33[33m»\33[31m Force interlaced display mode.\n");
  2103.             Printf("           \33[1mCYCLE\33[0m  \33[33m»\33[31m Force colour cycling on startup.\n\n");
  2104.  
  2105.             Printf("                    Note that \33[1mNOMODE\33[0m and \33[1mLACE\33[0m are \33[4mmutually exclusive\33[0m.\n\n");
  2106.  
  2107.             Printf(" If you wish to come in contact with \33[1mme\33[0m, here's my address:\n\n");
  2108.  
  2109.             Printf("\tOlaf 'Olsen' Barthel\n");
  2110.             Printf("\tBrabeckstrasse 35\n");
  2111.             Printf("\tD-3000 Hannover 71\n\n");
  2112.  
  2113.             Printf("\t\33[1mFederal Republic of Germany\33[0m\n\n");
  2114.  
  2115.             Printf(" Have \33[4mfun\33[0m with the code ... \33[3mOlsen\33[0m\n\n");
  2116.  
  2117.             CleanExit(0);
  2118.         }
  2119.  
  2120.             /* Both modes selected? */
  2121.  
  2122.         if(argv[ARG_NOMODE] && argv[ARG_LACE])
  2123.         {
  2124.             Puts(CLI_Help);
  2125.             CleanExit(RETURN_WARN);
  2126.         }
  2127.  
  2128.             /* Force scrolling? */
  2129.  
  2130.         if(argv[ARG_NOMODE])
  2131.         {
  2132.             ForceScroll = TRUE;
  2133.             Offset++;
  2134.         }
  2135.  
  2136.             /* Interlaced display mode? */
  2137.  
  2138.         if(argv[ARG_LACE])
  2139.         {
  2140.             ForceLace = TRUE;
  2141.             Offset++;
  2142.         }
  2143.  
  2144.             /* Cycle on startup? */
  2145.  
  2146.         if(argv[ARG_CYCLE])
  2147.         {
  2148.             Cycle = TRUE;
  2149.             Offset++;
  2150.         }
  2151.  
  2152.             /* No file names given? Show the file requester. */
  2153.  
  2154.         if(!argv[ARG_FILES])
  2155.         {
  2156.             if(Request = ArpAllocFreq())
  2157.             {
  2158.                 Request -> fr_Hail = "Select file to display";
  2159.  
  2160.                 FOREVER
  2161.                 {
  2162.                     if(FileRequest(Request))
  2163.                     {
  2164.                         strcpy(TempName,Request -> fr_Dir);
  2165.  
  2166.                         TackOn(TempName,Request -> fr_File);
  2167.  
  2168.                         if((Error = LoadImage(TempName,ForceScroll,ForceLace,Cycle,FALSE)) < 0 || BreakCheck())
  2169.                         {
  2170.                             if(BreakCheck())
  2171.                                 Puts("*** aborted.\a");
  2172.                             else
  2173.                                 Puts("");
  2174.  
  2175.                             break;
  2176.                         }
  2177.  
  2178.                         switch(Error)
  2179.                         {
  2180.                             case ERR_NOIFF:        Printf("\33[33mNot an IFF-ILBM file.\33[31m\a");
  2181.                                         break;
  2182.  
  2183.                             case ERR_NOCOLMAP:    Printf("\33[33mNo colour map.\33[31m\a");
  2184.                                         break;
  2185.  
  2186.                             case ERR_NOMEM:        Printf("\33[33mNot enough memory.\33[31m\a");
  2187.                                         break;
  2188.  
  2189.                             case ERR_DOS:        Printf("\33[33mError while reading.\33[31m\a");
  2190.                                         break;
  2191.  
  2192.                             default:        Printf("Done.");
  2193.                                         break;
  2194.                         }
  2195.  
  2196.                         Puts("");
  2197.                     }
  2198.                 }
  2199.             }
  2200.  
  2201.             CleanExit(RETURN_OK);
  2202.         }
  2203.  
  2204.         NumItems = argc - Offset;
  2205.  
  2206.             /* Scan the argument list. */
  2207.  
  2208.         for(i = 0 ; i < NumItems ; i++)
  2209.         {
  2210.             if(PreParse(argv[ARG_FILES + i],TempName))
  2211.             {
  2212.                 NumFiles = 0;
  2213.  
  2214.                 while((Pat = scdir(argv[ARG_FILES + i])) && NumFiles < 255)
  2215.                 {
  2216.                     if(!(FileArray[NumFiles] = (char *)malloc(strlen(Pat) + 1)))
  2217.                         break;
  2218.  
  2219.                     strcpy(FileArray[NumFiles++],Pat);
  2220.  
  2221.                     if(NumFiles == 2)
  2222.                         Printf("\n\33[3mScanning directory for \33[0m\33[1m%s\33[0m...\n\n",BaseName(argv[ARG_FILES + i]));
  2223.                 }
  2224.  
  2225.                 if(NumFiles > 1)
  2226.                     QuickSort(FileArray,NumFiles);
  2227.  
  2228.                 for(j = 0 ; j < NumFiles ; j++)
  2229.                 {
  2230.                     if(!FileArray[j] || !FileArray[j][0])
  2231.                         continue;
  2232.  
  2233.                     Printf("Picture \33[33mNº %03ld\33[31m, \33[1m\"%s\"\33[0m loading... ",j + 1,BaseName(FileArray[j]));
  2234.  
  2235.                     if((Error = LoadImage(FileArray[j],ForceScroll,ForceLace,Cycle,(i == NumFiles - 1))) < 0 || BreakCheck())
  2236.                     {
  2237.                         if(BreakCheck())
  2238.                             Puts("*** aborted.\a");
  2239.                         else
  2240.                             Puts("");
  2241.  
  2242.                         break;
  2243.                     }
  2244.  
  2245.                     switch(Error)
  2246.                     {
  2247.                         case ERR_NOIFF:        Printf("\33[33mNot an IFF-ILBM file.\33[31m\a");
  2248.                                     break;
  2249.  
  2250.                         case ERR_NOCOLMAP:    Printf("\33[33mNo colour map.\33[31m\a");
  2251.                                     break;
  2252.  
  2253.                         case ERR_NOMEM:        Printf("\33[33mNot enough memory.\33[31m\a");
  2254.                                     break;
  2255.  
  2256.                         case ERR_DOS:        Printf("\33[33mError while reading.\33[31m\a");
  2257.                                     break;
  2258.  
  2259.                         default:        Printf("Done.");
  2260.                                     break;
  2261.                     }
  2262.  
  2263.                     Puts("");
  2264.                 }
  2265.  
  2266.                 for(j = 0 ; j < NumFiles ; j++)
  2267.                     free(FileArray[j]);
  2268.             }
  2269.             else
  2270.             {
  2271.                 Printf("Picture \33[33mNº %03ld\33[31m, \33[1m\"%s\"\33[0m loading... ",i + 1,argv[ARG_FILES + i]);
  2272.  
  2273.                 if((Error = LoadImage(argv[ARG_FILES + i],ForceScroll,ForceLace,Cycle,(i == NumItems - 1))) < 0 || BreakCheck())
  2274.                 {
  2275.                     if(BreakCheck())
  2276.                         Puts("*** aborted.\a");
  2277.                     else
  2278.                         Puts("");
  2279.  
  2280.                     break;
  2281.                 }
  2282.  
  2283.                 switch(Error)
  2284.                 {
  2285.                     case ERR_NOIFF:        Printf("\33[33mNot an IFF-ILBM file.\33[31m\a");
  2286.                                 break;
  2287.  
  2288.                     case ERR_NOCOLMAP:    Printf("\33[33mNo colour map.\33[31m\a");
  2289.                                 break;
  2290.  
  2291.                     case ERR_NOMEM:        Printf("\33[33mNot enough memory.\33[31m\a");
  2292.                                 break;
  2293.  
  2294.                     case ERR_DOS:        Printf("\33[33mError while reading.\33[31m\a");
  2295.                                 break;
  2296.  
  2297.                     default:        Printf("Done.");
  2298.                                 break;
  2299.                 }
  2300.  
  2301.                 Puts("");
  2302.             }
  2303.         }
  2304.     }
  2305.  
  2306.     CleanExit(RETURN_OK);
  2307. }
  2308.